home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
7_10.lha
/
7_10
/
test4.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
3KB
|
93 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
* based on cfront 2.0 b5 demo program triv.C */
include <debug.h> /* DELETE */
*ident "@(#)ctrans:demo/task/triv.C 1.1" */
**************************************************************************
Copyright (c) 1984 AT&T
All Rights Reserved
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
The copyright notice above does not evidence any
actual or intended publication of such source code.
****************************************************************************/
include <process.h>
* trivial process example:
Make a set of processes which pass an object round between themselves.
Each process gets an object from the head of one queue, and puts
the object on the tail of another queue.
Main creates each process, then puts the object on a queue.
Each process quits after getting the object MAX_CYCLES times.
/
onst int NTASKS = 1;
onst int MAX_CYCLES = 5;
truct pc : process { // derive a class from process
queue_tail *t;
queue_head *h;
char *n;
pc(char*nn, queue_tail*nt, queue_head*nh) // process is not intended to be used directly
: (nn)
{
if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! new pc(" << nn << ")\n";
cout << "new pc(" << nn << ")\n";
t = nt; h = nh; n = nn;
}
long main();
;
ong pc::main() // process body serves as
// "main" program for process
for (int i = 0; i < MAX_CYCLES; i++) {
process_object* p = h->get();
if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! process " << n << "\n";
cout << "process " << n << "\n";
t->put(p);
}
if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! process " << n << ": done.\n";
cout << "process " << n << ": done.\n";
return 0; // Always end process constructors with return.
ain()
queue_head* hh = new queue_head;
queue_tail* t = hh->tail(); // hh and t refer to same queue.
queue_head* h;
if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! main\n";
cout << "main\n";
for (int i=0; i<NTASKS; i++) {
char* n = new char[2]; // make a one letter process name
n[0] = 'a'+i;
n[1] = 0;
h = new queue_head;
(new pc(n,t,h))->exec(); // create a new process and start execution
if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! main()'s loop\n";
cout << "main()'s loop\n";
t = h->tail();
}
(new pc("first pc",t,hh))->exec(); // create another new process
if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! main: here we go\n";
cout << "main: here we go\n";
t->put(new process_object); // put the object on a queue
if (debug) /*DELETE*/ cerr << "!!!!!!!!!!!!!!!! main: exit\n";
cout << "main: exit\n";
main_process()->exit(0); // main is a process too; it must also
// end with exit (to allow any
// remaining processes to run, otherwise
// the whole process would exit).
printf("should not get here\n");
return 0;